package demolist;

/**
 * @Author 12629
 * @Description：
 */
public class MySingleList {

    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;

    }

    public void show() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //求链表的长度
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //是否包含 数据key
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        //cur.next == null
        cur.next = node;
    }

    public void addIndex(int index,int val) {
        if(index < 0 || index > size()) {
            return;
        }
        if(index == 0) {
            addFirst(val);
            return;
        }
        if(index == size()) {
            addLast(val);
            return;
        }

        ListNode cur = findIndex(index);
        ListNode node = new ListNode(val);
        node.next = cur.next;
        cur.next = node;
    }

    /**
     * 找到index位置的前一个节点
     * @param index
     * @return
     */
    private ListNode findIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //删除第一个出现的关键字key
    public void remove(int key) {
        if(head == null) return;
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = findKey(key);
        if(cur == null) {
            return;//没有你要删除的数据
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }
    //找到删除的关键字key的前一个
    private ListNode findKey(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        //没有找到返回null
        return null;
    }

    public void removeAllKey(int key) {
        if(head == null) return;
        /*while(head.val == key) {
            head = head.next;
        }*/
        ListNode cur = head.next;
        ListNode prev = head;
        while (cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur =cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.val == key) {
            head = head.next;
        }
    }

    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }

    public ListNode reverseList() {
        if(head == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;
    }
    //从指定位置开始进行打印
    public void show(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 找到链表的中间节点
     * @return
     */
    public ListNode findMiddleNode() {
        if(head == null) {
            return null;
        }
        int len = size();
        ListNode cur = head;
        int count = 0;
        while (count != len/2) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    public ListNode findMiddleNode2() {
        ListNode fast = head;
        ListNode slow = head;
        //fast一定是 先走到链表尾巴的
        while (fast != null && fast.next != null) {
            //while (fast.next != null && fast != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }


    public int kthToLast( int k) {
        if(k <= 0 || k > size()) {
            return -1;
        }
        int len = size();
        int count = len-k;
        ListNode cur = head;
        while (count != 0) {
            cur = cur.next;
            count--;
        }
        return cur.val;
    }


    public int kthToLast2( int k) {
        if(k <= 0 ) {
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;

        int count = 0;
        while (count != k-1) {
            fast = fast.next;
            if(fast == null) {
                return -1;
            }
            count++;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    public ListNode partition( int x) {
        // write code here
        ListNode cur = head;

        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        while (cur != null) {
            if(cur.val < x) {
                //<x 放到左边
                //1.第一次插入
                if(bs == null) {
                    bs = be = cur;
                }else {
                    //2.不是第一次插入
                    be.next = cur;
                    be = be.next;
                }
            }else {
                //>=x 放到右边
                //1.第一次插入
                if(as == null) {
                    as = ae =cur;
                }else {
                    //2.不是第一次插入
                    ae.next =cur;
                    ae = ae.next;
                }

            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }

    public boolean chkPalindrome() {
        if(head == null) {
            return false;
        }
        //1.找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2. 反转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        //3.开始判断
        while (head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }


    public boolean hasCycle(ListNode head) {
        if(head == null) return false;
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                return true;
            }
        }
        return false;
    }

    public void createLoop() {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = head.next.next;
    }

    public ListNode detectCycle(ListNode head) {
        if(head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return null;//没有环
        }
        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}





